package com.typingtutor.ui.tabs;

import com.typingtutor.dto.FieldMappingDAO;
import com.typingtutor.ui.TypingTutor;
import lombok.Getter;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class DataTableTab {
    @Getter
    private JPanel panel;
    private final TypingTutor mainFrame;
    private DefaultTableModel tableModel;

    public DataTableTab(TypingTutor mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
//        List<Card> cards = queryCardsFromDatabase();
//        loadData(cards);
    }

    private void initComponents() {
        panel = new JPanel(new BorderLayout());
        tableModel = new DefaultTableModel(new String[]{"假名", "日本汉字", "词性", "中文含义", "复习间隔", "难易度"}, 0);
        JTable table = new JTable(tableModel);
        JScrollPane tableScrollPane = new JScrollPane(table);
        panel.add(tableScrollPane, BorderLayout.CENTER);

        JButton deleteButton = new JButton("删除全部数据库");
        deleteButton.addActionListener(new DeleteDatabaseListener());
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(deleteButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private class DeleteDatabaseListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String confirmText = JOptionPane.showInputDialog(mainFrame, "请输入“我已知晓，确定删除全部数据库”以确认删除操作：");
            if ("我已知晓，确定删除全部数据库".equals(confirmText)) {
                FieldMappingDAO.getInstance().deleteAllCards();
                JOptionPane.showMessageDialog(mainFrame, "数据库已清空！", "提示", JOptionPane.INFORMATION_MESSAGE);

                // 清空数据表格
                tableModel.setRowCount(0);

                // 清空打字练习模块的数据
//                TypingPracticeTab typingPracticeTab = mainFrame.getTypingPracticeTab();
//                typingPracticeTab.onDataImported(null);

                // 清空统计图表模块的数据
//                StatisticsChartTab statisticsChartTab = mainFrame.getStatisticsChartTab();
//                statisticsChartTab.clearData();
            } else {
                JOptionPane.showMessageDialog(mainFrame, "输入内容不正确，删除操作已取消。", "提示", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

}